Functions
All but the simplest of programs will need functions. These are sections of code which execute with (optional) parameters and an (optional) return value. One function you have seen is main. This takes no parameters, and returns no value. The following example defines a function called sqr, takes one parameter, squares the parameter, and returns the result:
def sqr(x)
{
return x^2;
}
The def indicates that a function is about to be defined. After the function name, there is a set of parenthesis which enclose the parameters, which are separated by commas. If there are no parameters, the parentheses must still exist. You can name the parameters like any variable, and you may use them in the same way -- you can even set their value. Then a statement follows, usually a block, which defines the function. Normally, no value is returned. To return a value, use the return operator as shown. This returns the value, and exits the function. You can also use the exit operator which returns from the function, but does not return a value.
After a function is defined, you call it in the same manner as println and input. Specifically, parentheses always follow the function name to differentiate them from variables, and any parameters are passed here in the same order as they are defined with def. println is an example of a function which does not return a value, and input is an example of one that does. If a value is not returned, you may not treat the function as if it did, or your program will bomb. If a value is returned, you may safely ignore it and treat the function as if it returned nothing. The following exemplifies our example function and how it is used to make a simple program:
def sqr(x)
{
return x^2;
}
def main()
{
print("Enter a number: ");
number = input();
println(number," squared is ",sqr(number));
}
This prompts for a number, and prints the squared result on the screen. You may nest function calls like so:
def sqr(x)
{
return x^2;
}
def main()
{
print("Enter a number: ");
println(sqr(input()));
}
You may also call functions within other functions. Unlike many programming languages, you may continue this indefinatly as long as there's enough memory. (For the technical reader, there is no separate stack memory and heap memory.) It's also OK for functions to call themselves, although you must be careful not to fall into an infinite loop. For example, look at this function:
def func(x)
{
println(x);
func(x+1);
}
Given a number, this begins to print every larger integer. After memory is exhausted, the program will end. However, having functions call themselves (known as recursion) can be quite useful. For example, here are two ways to make the factorial function (x factorial is x(x-1)(x-2)...(2)(1)):
def fact1(x)
{
if (x<2)
return 1;
return x*fact(x-1);
}
def fact2(x)
{
f = 1;
for(k in 2..x)
f = f*k;
return f;
}
As you can see, fact1 is easier to understand, more simple, and shorter then fact2.
One final but important note about functions: you may declare them anywhere, and use them anywhere. This means that, no matter where in your code you call a function, as long as that function is defined somewhere, everything's fine unlike many languages such as C and Pascal which require that the function be defined prior to its use. Not only does this make programming large project easier within one file, it facillitates modularized code. That is, you can write files with functions, and use the functions defined therein without header or definition files. Libraries are literally plug and play.
The next section in the Tutorial revists lists, this time with additional functions known as member functions which enhance the power of lists.
Web page maintained by Jason Cohen